<template>
  <el-dialog v-el-drag-dialog :title="!form.courseId?'新建课程':'编辑课程'" :visible.sync="visible" :append-to-body="appendToBody">
    <el-form ref="courseForm" :model="form" label-width="120px" :disabled="!hasLoaded">
      <el-card>
        <el-form-item hidden>
          <el-input v-model="form.courseId" hidden/>
          <el-input v-model="form.thumbnail" hidden/>
          <el-input v-model="form.classHour" hidden/>
        </el-form-item>
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="form.courseName"/>
        </el-form-item>
        <el-form-item label="课程封面">
          <file-viewer v-model="thumbnails" :file-data="thumbData" :accept="'image/jpg,image/jpeg,image/png'" :limit="1" @getFileData="getThumbData" />
        </el-form-item>
        <el-form-item label="课程简介" prop="shortDesc">
          <el-input
            v-model="form.shortDesc"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
        <el-form-item label="课程价格" prop="charge">
          <el-input-number
            style="width:120px"
            v-model="form.charge"
            :step="0.1"
            :min="0.1"
            :precision="2"
            controls-position="right"
          />
          <label class="m-l-10">元</label>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status">
            <el-option
              v-for="item in optionOfStatus"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import FileViewer from '@/components/FileViewer'
import { courseLoad, courseSave } from '@/api/training/course'
import { fileManageTable } from '@/api/tool/file'

export default {
  name: 'TrainingCourseFormDialog',
  components: { FileViewer },
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        courseId: undefined,
        courseName: undefined,
        thumbnail: undefined,
        charge: undefined,
        shortDesc: undefined,
        classHour: undefined,
        status: undefined
      },
      thumbnails: [],
      thumbData: [],
      optionOfStatus: []
    }
  },
  methods: {
    init(id) {
      this.hasLoaded = false
      this.form.courseId = id || null
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(this.form.courseId)
        this.getDictData()
      })
    },
    loadForm(id_key) {
      courseLoad({'id': id_key}).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        if (this.form.thumbnail) {
          this.thumbnails.push(response.param.thumbnail)
          this.getThumbData()
        }
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['course:course:edit'])
      }).catch(() => {
      })
    },
    getDictData() {
      this.$dictGroup('sys_publish_status').then(response => {
        this.optionOfStatus = response
      }).catch(e => {
      })
    },
    getThumbData() {
      fileManageTable({Q_fileId_S_IN: this.thumbnails.join(',')}).then(response => {
        if (response.param.data && response.param.data.length > 0) {
          this.thumbData = response.param.data
        }
      })
    },
    onSubmit() {
      this.$refs.courseForm.validate(valid => {
        if (valid) {
          if (this.thumbnails.length > 0) {
            this.form.thumbnail = this.thumbnails[0]
          }
          courseSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {
          })
        }
      })
    },
    onClose() {
      this.form = {
        courseId: undefined,
        courseName: undefined,
        thumbnail: undefined,
        charge: undefined,
        shortDesc: undefined,
        classHour: undefined,
        status: undefined
      }
      this.thumbnails = []
      this.thumbData = []
      this.visible = false
    }
  }
}
</script>
